package com.ljx.funchat.user.dao;

import com.ljx.funchat.user.domain.entity.User;
import com.ljx.funchat.user.domain.vo.req.UserInfoUpdateReq;
import com.ljx.funchat.user.mapper.UserMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author LiJunXi
 * @since 2024-02-13
 */
@Service
public class UserDao extends ServiceImpl<UserMapper, User> {

    public User getByOpenId(String openid) {
        return lambdaQuery()
                .eq(User::getOpenId, openid)
                .one();
    }

    public Integer countByName(String name) {
        return lambdaQuery().eq(User::getName, name)
                .count();
    }

    public List<User> getByFriendIds(List<Long> friendIds) {
        if(friendIds.isEmpty()) {
            return new ArrayList<>();
        }
        return lambdaQuery().in(User::getId, friendIds)
                .select(User::getId, User::getActiveStatus, User::getName, User::getAvatar)
                .list();
    }

    public void updateUserInfo(Long uid, UserInfoUpdateReq req) {
        lambdaUpdate().eq(User::getId, uid)
                .set(User::getAvatar, req.getAvatar())
                .set(User::getSex, req.getSex())
                .set(User::getName, req.getName())
                .update();
    }
}
